<template>
  <div class="container">
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="22">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="关键字">
              <el-input v-model="articlesData.keyword"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="articlesData.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align: right">
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2" style="text-align: right">
          <el-button
            icon="el-icon-edit"
            @click="openArticleDialog"
            type="success"
            size="small"
            >新增技巧</el-button
          >
        </el-col>
      </el-row>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共${counts}条`"
        style="margin-bottom: 15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="articlesList">
        <el-table-column
          prop=""
          label="序号"
          width="80px"
          type="index"
        ></el-table-column>
        <el-table-column prop="title" label="文章标题">
          <template #default="{ row }">
            {{ row.title }}
            <a
              v-if="row.videoURL"
              href="#"
              class="el-icon-film"
              @click="openVideo(row.videoURL)"
            ></a>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"></el-table-column>
        <el-table-column prop="username" label="录入人"></el-table-column>
        <el-table-column prop="createTime" label="录入时间">
          <template #default="{ row }">
            {{ row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template #default="{ row }">{{
            row.state === 1 ? "已启用" : "已禁用"
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button size="mini" type="text" @click="openPreview(row)"
              >预览</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-if="row.state == 1"
              @click="handleToggle(row)"
              >禁用</el-button
            >
            <el-button size="mini" type="text" v-else @click="handleToggle(row)"
              >启用</el-button
            >
            <el-button
              size="mini"
              type="text"
              :disabled="row.state === 1 ? true : false"
              @click="openArticleDialog(row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              :disabled="row.state === 1 ? true : false"
              @click="handleDel(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="text-align: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[1, 3, 5, 7, 10]"
        :page-size="pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="counts"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 视频 -->
    <div class="video-preview" v-if="videoURL">
      <div class="close" @click="closeVideo()">
        <span class="el-icon-close"></span>
      </div>
      <div class="box">
        <video ref="video" :src="videoURL" controls></video>
      </div>
    </div>
    <ArticlesPreview
      :isPreviewShow.sync="isPreviewShow"
      :currentArticle="currentArticle"
    />
    <ArticlesAdd
      ref="articlesAdd"
      :isDialogShow.sync="isDialogShow"
      :data="currArt"
    />
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles.js'
import ArticlesPreview from '../components/articles-preview.vue'
import ArticlesAdd from '../components/articles-add.vue'
export default {
  components: { ArticlesPreview, ArticlesAdd },
  data () {
    return {
      articlesData: {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      },
      articlesList: [],
      counts: 0,
      page: 1,
      pages: 0,
      pagesize: 10,
      videoURL: null,
      isPreviewShow: false,
      currentArticle: {},
      isDialogShow: false,
      articleID: null,
      currArt: {}
    }
  },
  created () {
    this.getArticlesList()
  },
  methods: {
    async getArticlesList () {
      const { data } = await list(this.articlesData)
      console.log(data)
      this.articlesList = data.items
      this.page = +data.page
      this.pagesize = +data.pagesize
      this.counts = data.counts
    },
    handleSizeChange (val) {
      this.articlesData.page = 1
      this.articlesData.pagesize = val
      this.getArticlesList()
    },
    handleCurrentChange (val) {
      this.articlesData.page = val
      this.getArticlesList()
    },
    // 搜索功能
    handleSearch () {
      this.page = 1
      this.getArticlesList()
    },
    // 清空
    clear () {
      this.articlesData = {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      }
    },
    openVideo (videoURL) {
      // console.log(videoURL)
      this.videoURL = videoURL
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    closeVideo () {
      console.log(1)
      this.videoURL = null
      this.$refs.video.pause()
    },
    // 预览
    openPreview (row) {
      this.isPreviewShow = true
      // console.log(row)
      this.currentArticle = row
    },
    // 启用/禁用 切换
    async handleToggle (row) {
      await changeState({ id: row.id, state: row.state === 1 ? 0 : 1 })
      row.state = row.state === 1 ? 0 : 1
    },
    openArticleDialog (row) {
      this.isDialogShow = true
      this.currArt = row
      this.$nextTick(() => {
        this.$refs.articlesAdd.open()
      })
    },
    // 删除
    handleDel (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        this.$message.success('删除成功')
        await remove({ id: id })
        this.getArticlesList()
      }).catch(() => {
        this.$message.info('取消删除')
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.el-icon-film {
  color: blue;
}
.video-preview {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .close {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
